%macro get_industry;

	proc printto new log = "&logdir./get_industry.txt";
	run;

		proc sort data = rlib.f941(keep=firm_id) nodupkey
			out = tinlist;
			by firm_id;
		run;
		data _null_;
			set tinlist;
			file "&tmpdir./tinlist" dlm=',';
			put firm_id;
		run;



		/****************************************************
		*													*
		*					Main SQL call					*
		*													*
		****************************************************/
		proc sql;
		connect to odbc as iq
			[redacted]
			(
				select
					b.firm_id,
					substr(a.[redacted],1,6) as naics6,
					case when a.[redacted] in("A","F","G","I","T")
						then 1 else 0 end as govt,
					count(*) as n
				from [redacted] as a
					inner join #tinlist as b
						on(a.[redacted] = b.firm_id)
				where (
					substr(a.[redacted],1,2) in(
					"11","21","22","23","31","32","33",
					"42","44","45","48","49","51","52",
					"53","54","55","56","61","62","71","72",
					"81","92")
					or a.[redacted] in("A","F","G","I","T")
				)
				and a.[redacted] >= 201800
				group by b.firm_id, naics6, govt
				order by b.firm_id, n

		);
	disconnect from iq;
	quit;

	data rlib.industry;
		set sample;
		by firm_id;
		if last.firm_id;
		drop n;
		
		if govt & substr(naics6,1,2) not in(
					"11","21","22","23","31","32","33",
					"42","44","45","48","49","51","52",
					"53","54","55","56","61","62","71","72",
					"81","92") then naics6 = "920000";
		
		
		naics3 = substr(naics6,1,3);
	run;

	proc printto;
	run;
	
	
%mend;